package com.example.forum.services.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.forum.mapper.AnnouncementMapper;
import com.example.forum.model.Announcement;
import com.example.forum.services.IAnnouncementService;

import lombok.extern.slf4j.Slf4j;

/**
 * 系统公告服务实现类
 */
@Service
@Slf4j
public class AnnouncementServiceImpl implements IAnnouncementService {

    private final AnnouncementMapper announcementMapper;
    
    public AnnouncementServiceImpl(AnnouncementMapper announcementMapper) {
        this.announcementMapper = announcementMapper;
    }

    @Override
    @Transactional
    public Long create(Announcement announcement) {
        try {
            announcementMapper.insert(announcement);
            log.info("创建公告成功，ID: {}", announcement.getId());
            return announcement.getId();
        } catch (Exception e) {
            log.error("创建公告失败: {}", e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public Announcement getById(Long id) {
        if (id == null || id <= 0) {
            log.warn("获取公告时ID无效: {}", id);
            return null;
        }
        
        try {
            Announcement announcement = announcementMapper.selectById(id);
            if (announcement == null) {
                log.info("未找到ID为{}的公告", id);
            }
            return announcement;
        } catch (Exception e) {
            log.error("获取公告失败，ID: {}, 错误: {}", id, e.getMessage(), e);
            return null;
        }
    }

    @Override
    @Transactional
    public boolean update(Announcement announcement) {
        if (announcement == null || announcement.getId() == null || announcement.getId() <= 0) {
            log.warn("更新公告时公告对象或ID无效");
            return false;
        }
        
        try {
            int rows = announcementMapper.update(announcement);
            boolean success = rows > 0;
            if (success) {
                log.info("更新公告成功，ID: {}", announcement.getId());
            } else {
                log.warn("更新公告未影响任何记录，ID: {}", announcement.getId());
            }
            return success;
        } catch (Exception e) {
            log.error("更新公告失败，ID: {}, 错误: {}", announcement.getId(), e.getMessage(), e);
            throw e;
        }
    }

    @Override
    @Transactional
    public boolean delete(Long id) {
        if (id == null || id <= 0) {
            log.warn("删除公告时ID无效: {}", id);
            return false;
        }
        
        try {
            int rows = announcementMapper.deleteById(id);
            boolean success = rows > 0;
            if (success) {
                log.info("删除公告成功，ID: {}", id);
            } else {
                log.warn("删除公告未影响任何记录，ID: {}", id);
            }
            return success;
        } catch (Exception e) {
            log.error("删除公告失败，ID: {}, 错误: {}", id, e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public List<Announcement> getActiveAnnouncements(int page, int size) {
        if (page < 1) page = 1;
        if (size < 1) size = 10;
        
        int offset = (page - 1) * size;
        
        try {
            List<Announcement> announcements = announcementMapper.selectActive(offset, size);
            log.info("获取有效公告列表成功，页码: {}, 每页条数: {}, 结果数量: {}", page, size, announcements.size());
            return announcements;
        } catch (Exception e) {
            log.error("获取有效公告列表失败，错误: {}", e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public List<Announcement> getAllAnnouncements(int page, int size) {
        if (page < 1) page = 1;
        if (size < 1) size = 10;
        
        int offset = (page - 1) * size;
        
        try {
            List<Announcement> announcements = announcementMapper.selectAll(offset, size);
            log.info("获取所有公告列表成功，页码: {}, 每页条数: {}, 结果数量: {}", page, size, announcements.size());
            return announcements;
        } catch (Exception e) {
            log.error("获取所有公告列表失败，错误: {}", e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public List<Announcement> getTopAnnouncements() {
        try {
            List<Announcement> announcements = announcementMapper.selectTopAnnouncements();
            log.info("获取置顶公告成功，结果数量: {}", announcements.size());
            return announcements;
        } catch (Exception e) {
            log.error("获取置顶公告失败，错误: {}", e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public int countActiveAnnouncements() {
        try {
            int count = announcementMapper.countActive();
            log.info("获取有效公告数量成功: {}", count);
            return count;
        } catch (Exception e) {
            log.error("获取有效公告数量失败，错误: {}", e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public int countAllAnnouncements() {
        try {
            int count = announcementMapper.countAll();
            log.info("获取所有公告数量成功: {}", count);
            return count;
        } catch (Exception e) {
            log.error("获取所有公告数量失败，错误: {}", e.getMessage(), e);
            throw e;
        }
    }

    @Override
    @Transactional
    public boolean view(Long id) {
        if (id == null || id <= 0) {
            log.warn("增加公告浏览量时ID无效: {}", id);
            return false;
        }
        
        try {
            int rows = announcementMapper.incrementViewCount(id);
            boolean success = rows > 0;
            if (success) {
                log.info("增加公告浏览量成功，ID: {}", id);
            } else {
                log.warn("增加公告浏览量未影响任何记录，ID: {}", id);
            }
            return success;
        } catch (Exception e) {
            log.error("增加公告浏览量失败，ID: {}, 错误: {}", id, e.getMessage(), e);
            throw e;
        }
    }

    @Override
    @Transactional
    public boolean publish(Long id) {
        if (id == null || id <= 0) {
            log.warn("发布公告时ID无效: {}", id);
            return false;
        }
        
        try {
            int rows = announcementMapper.updateStatus(id, (byte) 1);
            boolean success = rows > 0;
            if (success) {
                log.info("发布公告成功，ID: {}", id);
            } else {
                log.warn("发布公告未影响任何记录，ID: {}", id);
            }
            return success;
        } catch (Exception e) {
            log.error("发布公告失败，ID: {}, 错误: {}", id, e.getMessage(), e);
            throw e;
        }
    }

    @Override
    @Transactional
    public boolean draft(Long id) {
        if (id == null || id <= 0) {
            log.warn("将公告设为草稿时ID无效: {}", id);
            return false;
        }
        
        try {
            int rows = announcementMapper.updateStatus(id, (byte) 0);
            boolean success = rows > 0;
            if (success) {
                log.info("将公告设为草稿成功，ID: {}", id);
            } else {
                log.warn("将公告设为草稿未影响任何记录，ID: {}", id);
            }
            return success;
        } catch (Exception e) {
            log.error("将公告设为草稿失败，ID: {}, 错误: {}", id, e.getMessage(), e);
            throw e;
        }
    }

    @Override
    @Transactional
    public boolean expire(Long id) {
        if (id == null || id <= 0) {
            log.warn("将公告设为已过期时ID无效: {}", id);
            return false;
        }
        
        try {
            int rows = announcementMapper.updateStatus(id, (byte) 2);
            boolean success = rows > 0;
            if (success) {
                log.info("将公告设为已过期成功，ID: {}", id);
            } else {
                log.warn("将公告设为已过期未影响任何记录，ID: {}", id);
            }
            return success;
        } catch (Exception e) {
            log.error("将公告设为已过期失败，ID: {}, 错误: {}", id, e.getMessage(), e);
            throw e;
        }
    }

    @Override
    @Transactional
    public boolean setTop(Long id) {
        if (id == null || id <= 0) {
            log.warn("置顶公告时ID无效: {}", id);
            return false;
        }
        
        try {
            int rows = announcementMapper.updateTopStatus(id, (byte) 1);
            boolean success = rows > 0;
            if (success) {
                log.info("置顶公告成功，ID: {}", id);
            } else {
                log.warn("置顶公告未影响任何记录，ID: {}", id);
            }
            return success;
        } catch (Exception e) {
            log.error("置顶公告失败，ID: {}, 错误: {}", id, e.getMessage(), e);
            throw e;
        }
    }

    @Override
    @Transactional
    public boolean cancelTop(Long id) {
        if (id == null || id <= 0) {
            log.warn("取消置顶公告时ID无效: {}", id);
            return false;
        }
        
        try {
            int rows = announcementMapper.updateTopStatus(id, (byte) 0);
            boolean success = rows > 0;
            if (success) {
                log.info("取消置顶公告成功，ID: {}", id);
            } else {
                log.warn("取消置顶公告未影响任何记录，ID: {}", id);
            }
            return success;
        } catch (Exception e) {
            log.error("取消置顶公告失败，ID: {}, 错误: {}", id, e.getMessage(), e);
            throw e;
        }
    }
} 